import axios from "axios"
export default {
    namespaced: true,
    state: {
        carsList: [],
        reminder: "",
        carDetail: {},
        total: 0,
        currentStatus: "您的车辆",
        userList: [],
        usertotal: 0,
        filterPassCars: [],
        filterRentingCars: []
    },
    mutations: {
        //将返回的分页数据赋值给state
        bringCars(state, payload) {
            if (typeof payload == "string") {
                state.reminder = payload
            } else {
                state.carsList = payload.message
                state.total = payload.total
                //转换状态值
                state.carsList.map(item => {
                    switch (item.status) {
                        case 0:
                            item.transStatus = "已下架";
                            //审核annual是否开启
                            item.isChanged = true;
                            break;
                        case 1:
                            item.transStatus = "未审核";
                            item.isChanged = false;
                            break;
                        case 2:
                            item.transStatus = "审核通过";
                            item.isChanged = true;
                            break;
                        case 3:
                            item.transStatus = "拒绝通过";
                            item.isChanged = true;
                            break;
                        case 4:
                            item.transStatus = "待出租";
                            item.isChanged = true;
                            break;
                        case 5:
                            item.transStatus = "已出租";
                            item.isChanged = true;
                            break;
                        default:
                            item.transStatus = "未审核";
                            item.isChanged = false;
                            break;
                    }
                    return item
                });
                //转换年检状态
                state.carsList.map(item => {
                    switch (item.carCondition.yearCheck) {
                        case false:
                            item.transCheck = "未通过";
                            break;
                        default:
                            item.transCheck = "通过";
                            break;
                    }
                    return item
                });
            }
        },
        //用户获取车辆审核状态
        getOneCarStatus(state, payload) {
            switch (payload.status) {
                case 0:
                    state.currentStatus = state.currentStatus + "已下架";
                    break;
                case 1:
                    item.transStatus = state.currentStatus + "未审核";
                    break;
                case 2:
                    item.transStatus = state.currentStatus + "审核通过";
                    break;
                case 3:
                    item.transStatus = state.currentStatus + "拒绝通过";
                    break;
                case 4:
                    item.transStatus = state.currentStatus + "待出租";
                    break;
                case 5:
                    item.transStatus = state.currentStatus + "已出租";
                    break;
                default:
                    item.transStatus = state.currentStatus + "未审核";
                    break;
            }
        },
        //修改状态
        upDateStatusMutation(state, payload) {
            console.log(payload);
            state.carsList = state.carsList.map(item => {
                if (item._id == payload._id) {
                    switch (payload.status) {
                        case 2:
                            item.transStatus = "审核通过";
                            item.isChanged = true;
                            return item;
                        case 3:
                            item.transStatus = "拒绝通过";
                            item.isChanged = true;
                            return item;
                        case 4:
                            item.transStatus = "待出租";
                            item.isChanged = true;
                            return item;
                        default:
                            item.transStatus = "未审核";
                            return item;
                    }

                }
                return item;
            })
        },
        //用户获取自己的车辆
        userSearchMutation(state, payload) {
            state.userList = payload.message.filter(item => !item.isRemoved)
            state.usertotal = payload.total
        },
        //筛选审核通过的车辆
        filterPass(state, payload) {
            state.filterPassCars = payload.filter(item => !item.isRemoved && item.status === 2)
            console.log(state.filterPassCars);
        },
        //在租状态的车辆
        filterRenting(state, payload) {
            state.filterRentingCars = payload.filter(item => item.status === 4);
            state.filterRentingCars = state.filterRentingCars.map(item => {
                item.transStatus = "待出租";
                item.transCheck = "通过";
                return item
            })
        }
    },
    actions: {
        //分页获取数据
        getCars(context, { start = 0, count = 3 } = {}) {
            axios.get("/car/getCars", { params: { start, count } }).then(({ data }) => {
                context.commit("bringCars", data)
            })
        },
        //获取某一车辆详情,可以用于刷新获取状态
        getOneCar(context, _id) {
            axios.get("/car/carDetail", { params: { _id } }).then(({ data }) => {
                context.commit("getOneCarDetail", data[0])
            })
        },
        //修改状态
        upDateStatus(context, { _id, status }) {
            console.log("state" + status);
            axios.put("/car/upDateStatus", { _id, status }).then(({ data }) => {
                //修改状态
                if (data) {
                    context.commit("upDateStatusMutation", { _id, status })
                }
            })
        },
        //用户搜索车辆
        userSearch(context, { selectedType = "", selectedValue = "", userId = "5e9fdd4a9b630301708e8e38" } = {}) {
            axios.get("/car/search", { params: { selectedType, selectedValue, userId } }).then(({ data }) => {
                context.commit("userSearchMutation", data)
                context.commit("filterPass", data.message)
            })
        },
        //用户移除下架车辆
        userRemove(context, { _id = "5e9ec31c0b2a0d075c5c4ad9" } = {}) {
            axios.put("/car/upDateRemoved", { _id }).then(({ data }) => {
                console.log(data);

            })
        }
    },
}


